﻿using Newtonsoft.Json.Linq;
using System;
using System.Threading.Tasks;

namespace xDataSet
{
    /// <summary>
    /// 缓存数据库操作
    /// 作者：徐锡平
    /// 时间：2015.10.09
    /// </summary>
    public class QueryCache : StackExchangeRedisHelper
    {
        /// <summary>
        /// 获取缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String GetStringByKey(string key)
        {
            try
            {
                String str = GetString(Config.RedisCacheDBNo, key);
                if (String.IsNullOrEmpty(str))
                {
                    return "";
                }
                else
                {
                    return str;
                }
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 设置用户登陆信息的缓存时间
        /// </summary>
        /// <param name="key"></param>
        public static void SetUserLoginCacheExpire(string key)
        {
            Task t = new Task(() =>
               {
                   SetStringExpire(Config.LoginStateDBNo, key, Config.LoginStateCacheTimeOut * 60);
               });
            t.Start();
        }
        /// <summary>
        /// 添加用户登陆信息到缓存
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        public static void AddUserLoginState(string userid, string state)
        {
            try
            {
                JObject jState = new JObject();
                jState["LEVEL"] = state;
                StackExchangeRedisHelper.SetString(Config.LoginStateDBNo, userid, jState.ToString(), Config.LoginStateCacheTimeOut * 60);
            }
            catch
            {

            }
        }
        /// <summary>
        /// 获取用户登陆缓存信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static JObject GetUserLoginStateValueByKey(string userId)
        {
            String str = GetString(Config.LoginStateDBNo, userId);
            if (String.IsNullOrEmpty(str))
            {
                return null;
            }
            else
            {
                return JObject.Parse(str);
            }
        }
        /// <summary>
        /// 获取缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static String GetStringByKey(string key, int db)
        {
            try
            {
                String str = GetString(db, key);
                if (String.IsNullOrEmpty(str))
                {
                    return "";
                }
                else
                {
                    return str;
                }
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 保存缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetStringInfo(string key, string value)
        {
            try
            {
                insertInfo(key, value, Config.RedisCacheDBNo);
            }
            catch
            {
            }
        }
        /// <summary>
        /// 保存缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="minutes"></param>
        public static void SetStringInfo(string key, string value, int minutes)
        {
            try
            {
                    insertInfo(key, value, Config.RedisCacheDBNo, minutes);
            }
            catch
            {
            }
        }
        /// <summary>
        /// 保存缓存信息
        /// </summary>
        /// <param name="db"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetStringInfo(int db, string key, string value)
        {
            try
            {
                Task t = new Task(() =>
                {
                    insertInfo(key, value, db);
                });
                t.Start();
            }
            catch
            {

            }
        }
        /// <summary>
        /// 保存缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="info"></param>
        /// <param name="db"></param>
        /// <param name="expire"></param>
        private static void insertInfo(string key, string info, int db, int expire = 0)
        {
            if (expire > 0)
            {
                StackExchangeRedisHelper.SetString(db, key, info, expire);
            }
            else
            {
                StackExchangeRedisHelper.SetString(db, key, info, Config.CacheExpireTime * 60);
            }
        }

        


    }
}
